wx.cloud.init({
  env: 'database-2p2zg',
  traceUser:true
})
const db = wx.cloud.database()
const person = db.collection('person')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!wx.getStorageSync('userInfo')) {
      this.setData({ authorize: true, signIn: false })
    }
    else {
      this.setData({ authorize: false, signIn: true })
    }
    this.data.openid = options.id
    // 从其它页面进入
    db.collection('person').where({
      _openid: options.id
    }).get()
      .then(res=>{
        var data = res.data[0]
        try{
          if(data.gender == 1 ){
            var gender = '/images/male.png'
          }
          else if(data.gender == 2){
            var gender = '/images/female.png'
          }
        }
        catch(e){
          var gender = ''
        }
        this.data.imagesUrl = data.images
        this.setData({
          avatar:data.avatarImage,
          name:data.name,
          faculty: data.faculty,
          major:data.major,
          id: options.id,
          hobby:data.hobby,
          xingge:data.xingge,
          contactType:data.contactType,
          contactInfo:data.contactInfo,
          images: data.images,
          openid: data._openid,
          type:options.type,
          grade: data.grade,
          gender: gender
        })
      })  
  },

  previewImages: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.imageurl,
      urls: this.data.imagesUrl,
    })
  },

  getuserinfo(res) {
    if (res.detail.userInfo) {
      wx.setStorageSync('userInfo', res.detail.userInfo)
      var user = res.detail.userInfo
      person.add({
        data: {
          name: user.nickName,
          avatarImage: user.avatarUrl,
          province: user.province,
          city: user.city,
          gender: user.gender
        }
      })
      wx.navigateTo({
        url: '/pages/editMe/editMe?id=' + this.data.openid,
      })
    }
  },

  onShareAppMessage: function () {
    return {
      title: '校园互助，方便你我',
      path: 'pages/personal/personal?id=' + this.data.openid,
    }
  },
})